/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SuppliersBeans;

import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import org.hibernate.criterion.Restrictions;
import org.orm.PersistentException;
import smartgrid.Contract;
import smartgrid.ContractsManager;
import smartgrid.DataLine;
import smartgrid.TariffCriteria;
import smartgrid.exceptions.InvalidContractNifException;
import smartgrid.exceptions.InvalidContractNumberException;

/**
 *
 * @author claudio
 */
@Stateless
public class EDPbean implements EDPbeanLocal {

    Integer supplierID = 1;

    @Override
    public Contract getContract(String number, String nif) throws InvalidContractNumberException, InvalidContractNifException {
        //throw new InvalidContractNumberException();
        Random rand = new Random();
        rand.setSeed(System.nanoTime());
        Contract c = new Contract();

        // Simular nao existencia de contrato ou nif falhado 
        int falha = rand.nextInt(10);
        if (falha == 1) {
            throw new InvalidContractNumberException();
        } else if (falha == 2) {
            throw new InvalidContractNifException();
        }
        // ************ Insere contrato na BD ***************** 

        // Gera uma Tarifa 

        try {
            TariffCriteria tarifCrit;
            tarifCrit = new TariffCriteria();
            tarifCrit.add(Restrictions.eq("Supplier_Tariffs_role.id", supplierID));
            tarifCrit.add(Restrictions.sqlRestriction("1=1 ORDER BY RANDOM() LIMIT 1"));
            smartgrid.Tariff tariff = smartgrid.Tariff.loadTariffByCriteria(tarifCrit);

            // Gera um cliente 
            String clientName = Generator.generateName();
            String clientAddress = Generator.generateAddress();

            // Cria novo contrato 
            c.setClientName(clientName);
            c.setMorada(clientAddress);
            c.setClientNif(nif);
            c.setNumber(number);
            c.setSupplier_role(smartgrid.Supplier.getSupplierByORMID(supplierID));
            c.setTariff_CurrentTarif_role(tariff);

        } catch (PersistentException ex) {
            Logger.getLogger(EDPbean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return c;
    }

// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
    @Override
    public DataLine[] getHistory(Contract contract) {
        return null;
    }

    /*       PARA TESTES             */
    public static void main(String[] args) {
        try {
            EDPbean be = new EDPbean();
            Contract c = be.getContract("1223", "812337");
            if (c == null) {
                System.out.println(c);
            } else {
                System.out.println("Numero: " + c.getNumber());
                System.out.println("ClientName: " + c.getClientName());
                System.out.println("NIF: " + c.getClientNif());
                System.out.println("SupplierID: " + c.getSupplier_role().getName());
                System.out.println("TarifID: " + c.getTariff_CurrentTarif_role().getName());
            }
        } catch (InvalidContractNumberException ex) {
            Logger.getLogger(EDPbean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidContractNifException ex) {
            Logger.getLogger(EDPbean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
